//
//  WZPSeeBigPictureController.m
//  WZPItem
//
//  Created by 王王喆平 on 16/7/17.
//  Copyright © 2016年 UI. All rights reserved.
//

#import "WZPSeeBigPictureController.h"
#import "WZPThemeItem.h"
#import <UIImageView+WebCache.h>
#import <SVProgressHUD.h>
#import <Photos/Photos.h>

@interface WZPSeeBigPictureController()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic)  UIImageView *imageView;

@end

@implementation WZPSeeBigPictureController
- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
- (IBAction)save:(id)sender {
    // 将一张图片保存系统中去
//    UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image:didFinishSavingWhithError:contextInfo:), nil);
    // 将图片保存到自定义的相册中
    //用户定义是否可以保存
    switch ([PHPhotoLibrary authorizationStatus]) {
        case PHAuthorizationStatusNotDetermined:{
            // 不确定,询问是否保存
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                if (status == PHAuthorizationStatusAuthorized) {
                    // 保存图片
                    [self savePhotots];
                }
            }];
             break;
        }
           
        case PHAuthorizationStatusAuthorized:{
            [self savePhotots];
            break;
        }
        default:{
        // 拒绝,告知用户怎么样可以允许访问
            [SVProgressHUD showSuccessWithStatus:@"设置 - 百思不得其姐 - 打开允许访问相册开关"];
            
        }
            break;
    }
    [self savePhotots];
}
-(void)savePhotots{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    
    // 创建自定相册
    PHAssetCollectionChangeRequest * changRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:@"百思不得其姐"];
    // 创建图片请求类
    PHAssetCreationRequest * createRequest = [PHAssetCreationRequest creationRequestForAssetFromImage:_imageView.image];
    // 将图片添加到相册中
    [changRequest addAssets:@[createRequest.placeholderForCreatedAsset]];
    
} completionHandler:^(BOOL success, NSError * _Nullable error) {
    
    if(success){
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }else{
        [SVProgressHUD showErrorWithStatus:@"保存失败"];
    }

}];
}

// 点击保存时调用
//-(void)image:(UIImage *)image didFinishSavingWhithError:(NSError *)error contextInfo:(void *)contextInfo{
//    if (error) {
//        [SVProgressHUD showErrorWithStatus:@"保存失败"];
//    }else{
//    
//        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
//    }
//
//}

-(void)viewDidLoad{
    [super viewDidLoad];
    // 展示图片
    UIImageView * imageView = [[UIImageView alloc]init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:_item.image0]];
    _imageView = imageView;
    [_scrollView addSubview:imageView];
    
    CGFloat w = WZPScreenW;
    CGFloat h = w / _item.width * _item.height;
    imageView.frame = CGRectMake(0, 0, w, h);
    
    if (!_item.is_bigPicture) {
        imageView.center = CGPointMake(WZPScreenW * 0.5, WZPScreenH * 0.5);
        
    }else{
        [imageView sd_setImageWithURL:[NSURL URLWithString:_item.image1]];
        _scrollView.contentSize = CGSizeMake(0, h);
    
    }

    // 对图片进行缩放
    _scrollView.delegate = self;
    // 设置缩放比例
    if (_item.height / h > 1) {
        _scrollView.maximumZoomScale = _item.height / h;
    }
    _scrollView.minimumZoomScale = 1;

}
// 进行缩放时,scrollView 会询问代理 那个View 要缩放
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return _imageView;

}
@end
